package com.fishery.processing.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fishery.entity.GlobalException;
import com.fishery.entity.StatusCode;
import com.fishery.processing.dao.CraftDao;
import com.fishery.processing.dao.FactoryDao;
import com.fishery.processing.dao.InputDao;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.entity.Factory;
import com.fishery.processing.entity.Input;
import com.fishery.processing.mapper.InputMapper;
import com.fishery.processing.service.InputService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Service
public class InputServiceImpl extends ServiceImpl<InputMapper, Input> implements InputService {

    @Autowired
    private InputDao inputDao;

    @Autowired
    private CraftDao craftDao;

    @Autowired
    private FactoryDao factoryDao;

    public int createInput(Input input){
        Factory factory = factoryDao.findFactoryById(input.getProcessingFactoryId());
        if (factory == null){
            throw new GlobalException(StatusCode.ERROR,"加工厂不存在，无法添加投入品");
        }
        //id置空 等待mybatis-plus填充
        input.setId(null);
        return inputDao.addInput(input);
    }

    public int deleteInput(String inputId){
        return inputDao.remove(inputId);
    }

    public int updateInput(Input input){
        return inputDao.update(input);
    }

    public Input searchInputById(String inputId){
        return inputDao.findInputById(inputId);
    }

    public IPage<Input> searchInputsOfCraft(String craftId,Integer page,Integer size){
        return inputDao.findInputsOfCraft(craftId,page,size);
    }

    public List<Input> searchInputsOfCraft(String craftId){
        return inputDao.findInputsOfCraft(craftId);
    }

    public IPage<Input> searchInputsOfFactory(String factoryId,Integer page,Integer size){
        return inputDao.findInputsOfFactory(factoryId,page,size);
    }

    public List<Input> searchInputsOfFactory(String factoryId){
        return inputDao.findInputsOfFactory(factoryId);
    }
}
